package com.creatapark.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.creatapark.monitor.annotation.Monitor;
import com.creatapark.service.ReportService;
import com.creatapark.vo.LineDataVo;
import com.creatapark.vo.PieDataVo;
import com.creatapark.vo.TargetVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.creatapark.common.RestResult;

/**
 * ParkReportController
 *
 * @author Bruce01.fan
 * @date 2017/7/21
 */
@Controller
@RequestMapping("/report")  
public class ParkReportController {

    @Autowired
    ReportService reportService;
    
    @RequestMapping("/countByTimeReport")
    public String countByTimeReport() {
        return "countByTimeReport";
    }

    @RequestMapping("/getCountParkCarByTime")  
    @ResponseBody
    public RestResult getCountParkCarByTime(HttpServletRequest request) {  
    	String staTime = request.getParameter("staTime");
    	String endTime = request.getParameter("endTime");
    	String parkId = request.getParameter("parkId");
    	Map<Integer,Integer> result = reportService.countParkCarByTimeReport(staTime, endTime, Long.valueOf(parkId));
        return new RestResult(result);  
    }

    @RequestMapping("/enterParkCarByDeduration")
    @Monitor
    public String enterParkCarByDeduration() {
        return "derationReport";
    }

    @RequestMapping("/getCountParkCarByDeduration")
    @ResponseBody
    public RestResult getCountParkCarByDeduration(HttpServletRequest request) {
        String staTime = request.getParameter("staTime");
        String endTime = request.getParameter("endTime");
        String parkId = request.getParameter("parkId");
        String duration = request.getParameter("duration");
        String queryType = request.getParameter("queryType");

        try {
           if("1".equals(queryType)){
               return new RestResult(reportService.queryDurationReportByDay(Long.parseLong(parkId),staTime,endTime,Double.parseDouble(duration)));
           }else {
               return new RestResult(reportService.queryDurationReportByDays(Long.parseLong(parkId),staTime,endTime,Double.parseDouble(duration)));
           }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return new RestResult();
    }

    @RequestMapping("/hello")
    public String welcome(ModelMap model) {
        model.put("time", new Date());
        model.put("message","hello world!!");
        return "hello";
    }

    @RequestMapping("/getParkTotal")
    @ResponseBody
    public RestResult getParkTotal(HttpServletRequest request) {

        String staTime = request.getParameter("staTime");
        String endTime = request.getParameter("endTime");
        String parkId = request.getParameter("parkId");

        Assert.hasText(parkId,"parkId must not be empty.");
        Assert.hasText(staTime,"staTime must not be empty.");
        Assert.hasText(endTime,"endTime must not be empty.");

        List<TargetVo> targetVos = reportService.queryParkData(Long.parseLong(parkId),staTime,endTime);

        return new RestResult(targetVos);
    }

    @RequestMapping("/getPieData")
    @ResponseBody
    public RestResult getPieData(HttpServletRequest request) {

        String staTime = request.getParameter("staTime");
        String endTime = request.getParameter("endTime");
        String parkId = request.getParameter("parkId");
        String typeId = request.getParameter("typeId");

        Assert.hasText(parkId,"parkId must not be empty.");
        Assert.hasText(staTime,"staTime must not be empty.");
        Assert.hasText(endTime,"endTime must not be empty.");
        Assert.hasText(typeId,"typeId must not be empty.");

        PieDataVo pieDataVo = reportService.queryPieDataByType(Long.parseLong(parkId),staTime,endTime,typeId);

        return new RestResult(pieDataVo);
    }

    @RequestMapping("/getCompareLineData")
    @ResponseBody
    public RestResult getCompareLineData(HttpServletRequest request) {

        String staTime = request.getParameter("staTime");
        String endTime = request.getParameter("endTime");
        String parkId = request.getParameter("parkId");
        String typeId = request.getParameter("typeId");

        Assert.hasText(parkId,"parkId must not be empty.");
        Assert.hasText(staTime,"staTime must not be empty.");
        Assert.hasText(endTime,"endTime must not be empty.");
        Assert.hasText(typeId,"typeId must not be empty.");

        LineDataVo lineDataVo = reportService.queryLineDataByType(Long.parseLong(parkId),staTime,endTime,typeId);

        return new RestResult(lineDataVo);
    }

}
